﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Drawing;
using System.Windows.Forms;
using System.Text;

namespace DnPakExplorer {

	/// <summary>
	/// 九宫格图片
	/// </summary>
	public class Sprite {
		//public static void OnDraw(Graphics g, Image image, Rectangle dest, Rectangle destContent, Rectangle sprite) {
		//	// Center
		//	g.DrawImage(image, destContent, sprite, GraphicsUnit.Pixel);
		//	// TopLeft
		//	g.DrawImage(image,
		//		new Rectangle(dest.X, dest.Y, destContent.X-dest.X, destContent.Y-dest.Y),
		//		new Rectangle(0, 0, sprite.X, sprite.Y),
		//		GraphicsUnit.Pixel);
		//	// TopRight
		//	g.DrawImage(image,
		//		new Rectangle(destContent.Right, dest.Y, dest.Right-destContent.Right, destContent.Y-dest.Y),
		//		new Rectangle(sprite.Right, 0, image.Width-sprite.Right, sprite.Y),
		//		GraphicsUnit.Pixel);
		//	// BottomRight
		//	g.DrawImage(image,
		//		new Rectangle(destContent.Right, destContent.Bottom, dest.Right-destContent.Right, dest.Bottom-destContent.Bottom),
		//		new Rectangle(sprite.Right, sprite.Bottom, image.Width-sprite.Right, image.Height-sprite.Bottom),
		//		GraphicsUnit.Pixel);
		//	// BottomLeft
		//	g.DrawImage(image,
		//		new Rectangle(dest.X, destContent.Bottom, destContent.X-dest.X, dest.Bottom-destContent.Bottom),
		//		new Rectangle(0, sprite.Bottom, sprite.X, image.Height-sprite.Bottom),
		//		GraphicsUnit.Pixel);
		//	// Left
		//	g.DrawImage(image,
		//		new Rectangle(dest.X, destContent.Y, destContent.X-dest.X, destContent.Height),
		//		new Rectangle(0, sprite.Y, sprite.X, sprite.Height),
		//		GraphicsUnit.Pixel);
		//	// Top
		//	g.DrawImage(image,
		//		new Rectangle(destContent.X, dest.Y, destContent.Width, destContent.Y-dest.Y),
		//		new Rectangle(sprite.X, 0, sprite.Width, sprite.Y),
		//		GraphicsUnit.Pixel);
		//	// Right
		//	g.DrawImage(image,
		//		new Rectangle(destContent.Right, destContent.Y, dest.Right-destContent.Right, destContent.Height),
		//		new Rectangle(sprite.Right, sprite.Y, image.Width-sprite.Right, sprite.Height),
		//		GraphicsUnit.Pixel);
		//	// Bottom
		//	g.DrawImage(image,
		//		new Rectangle(destContent.X, destContent.Bottom, destContent.Width, dest.Bottom-destContent.Bottom),
		//		new Rectangle(sprite.X, sprite.Bottom, sprite.Width, image.Height-sprite.Bottom),
		//		GraphicsUnit.Pixel);
		//}

		[Category("fa")]
		public Image Image { get; set; }

		[Category("fa")]
		public Rectangle SpriteRect { get; set; }

		public Sprite(Image image, int l, int t, int r, int b) {
			Image = image;
			SpriteRect = new Rectangle(l, t, image.Width-l-r, image.Height-t-b);
		}

		public void Draw(Graphics g, Rectangle dest) {
			Draw(g, Image, dest, SpriteRect);
		}

		/// <summary>
		/// 绘制九宫格图片
		/// </summary>
		/// <param name="g">目标图像接口</param>
		/// <param name="image">目标原图</param>
		/// <param name="dest">屏幕目标边界矩形</param>
		/// <param name="destContent">屏幕目标内矩形</param>
		/// <param name="sprite">九宫格图片内矩形</param>
		/// <param name="imageRect">九宫格图片外矩形</param>
		public static void Draw(Graphics g, Image image, Rectangle dest, Rectangle destContent, Rectangle sprite, Rectangle imageRect) {
			// Center
			g.DrawImage(image, destContent, sprite, GraphicsUnit.Pixel);
			// TopLeft
			g.DrawImage(image,
				new Rectangle(dest.X, dest.Y, destContent.X-dest.X, destContent.Y-dest.Y),
				new Rectangle(imageRect.X, imageRect.Y, sprite.X-imageRect.X, sprite.Y-imageRect.Y),
				GraphicsUnit.Pixel);
			// TopRight
			g.DrawImage(image,
				new Rectangle(destContent.Right, dest.Y, dest.Right-destContent.Right, destContent.Y-dest.Y),
				new Rectangle(sprite.Right, imageRect.Y, imageRect.Right-sprite.Right, sprite.Y-imageRect.Y),
				GraphicsUnit.Pixel);
			// BottomRight
			g.DrawImage(image,
				new Rectangle(destContent.Right, destContent.Bottom, dest.Right-destContent.Right, dest.Bottom-destContent.Bottom),
				new Rectangle(sprite.Right, sprite.Bottom, imageRect.Right-sprite.Right, imageRect.Bottom-sprite.Bottom),
				GraphicsUnit.Pixel);
			// BottomLeft
			g.DrawImage(image,
				new Rectangle(dest.X, destContent.Bottom, destContent.X-dest.X, dest.Bottom-destContent.Bottom),
				new Rectangle(imageRect.X, sprite.Bottom, sprite.X-imageRect.X, imageRect.Bottom-sprite.Bottom),
				GraphicsUnit.Pixel);
			// Left
			g.DrawImage(image,
				new Rectangle(dest.X, destContent.Y, destContent.X-dest.X, destContent.Height),
				new Rectangle(imageRect.X, sprite.Y, sprite.X-imageRect.X, sprite.Height),
				GraphicsUnit.Pixel);
			// Top
			g.DrawImage(image,
				new Rectangle(destContent.X, dest.Y, destContent.Width, destContent.Y-dest.Y),
				new Rectangle(sprite.X, imageRect.Y, sprite.Width, sprite.Y-imageRect.Y),
				GraphicsUnit.Pixel);
			// Right
			g.DrawImage(image,
				new Rectangle(destContent.Right, destContent.Y, dest.Right-destContent.Right, destContent.Height),
				new Rectangle(sprite.Right, sprite.Y, imageRect.Right-sprite.Right, sprite.Height),
				GraphicsUnit.Pixel);
			// Bottom
			g.DrawImage(image,
				new Rectangle(destContent.X, destContent.Bottom, destContent.Width, dest.Bottom-destContent.Bottom),
				new Rectangle(sprite.X, sprite.Bottom, sprite.Width, imageRect.Bottom-sprite.Bottom),
				GraphicsUnit.Pixel);
		}

		public static void Draw(Graphics g, Image image, Rectangle dest, Rectangle sprite) {
			Draw(g, image, dest,
				new Rectangle(
					dest.X+sprite.X, 
					dest.Y+sprite.Y,
					dest.Width-sprite.X-(image.Width-sprite.Right),
					dest.Height-sprite.Y-(image.Height-sprite.Bottom)
				),
				sprite,
				new Rectangle(0, 0, image.Width, image.Height)
			);
		}

		public static void Draw(Graphics g, Image image, Rectangle dest, Padding spritePadding) {
			Draw(g, image, dest,
				new Rectangle(
					dest.X+spritePadding.Left,
					dest.Y+spritePadding.Top,
					dest.Width-spritePadding.Horizontal,
					dest.Height-spritePadding.Vertical
				),
				new Rectangle(
					spritePadding.Left,
					spritePadding.Top,
					image.Width - spritePadding.Horizontal,
					image.Height - spritePadding.Vertical
					),
				new Rectangle(0, 0, image.Width, image.Height)
			);
		}

		public static void Draw(Graphics g, Image image, Rectangle dest, Rectangle sprite, Rectangle imageRect) {
			Draw(g, image, dest,
				new Rectangle(
					dest.X+(sprite.X-imageRect.X),
					dest.Y+(sprite.Y-imageRect.Y),
					dest.Width-(sprite.X-imageRect.X)-(imageRect.Right-sprite.Right),
					dest.Height-(sprite.Y-imageRect.Y)-(imageRect.Bottom-sprite.Bottom)
				),
				sprite, imageRect
			);
		}

	}
	
}
